package bookstore.action;
import bookstore.iservice.*;
import bookstore.service.*;
import bookstore.dao.*;
import bookstore.model.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport
{  private ICustomerService bsCustomerService =new CustomerService();
   private Customer bsCustomer=new Customer();
   private String password;
   private String msg;
   public String getPassword(){return password;}
   public void setPassword(String password){this.password=password;}
   public void validateLogin()
   {
       if((bsCustomer.getName()==null)||bsCustomer.getName().equals(""))
       {
           this.addFieldError("bsCustomer.name", "<br/>账户不能为空！ ");
       }
       if((bsCustomer.getPassword()==null)||bsCustomer.getPassword().equals(""))
       {
           this.addFieldError("bsCustomer.password", "<br/>密码不能为空！ ");
       }
       
   }
   public String login() throws Exception
   {   
       bsCustomerService.setCustomerDao(new CustomerDao());
       bsCustomer=bsCustomerService.findCustomerByNameAndPassword(
                              bsCustomer.getName(),
                              bsCustomer.getPassword());
       if(bsCustomer==null)
       {
           ActionContext.getContext().getSession().put("msg","the Current User is Guest");
           msg=ERROR;
       }       
       else
         {
             msg=SUCCESS;
             ActionContext.getContext().getSession().put("bsCustomer",bsCustomer);
          }
       return msg;
   }
   public void validateRegister()
   {
       if((bsCustomer.getName()==null)||bsCustomer.getName().equals(""))
       {
           this.addFieldError("bsCustomer.name", "<br/>账户不能为空！ ");
       }
       if((bsCustomer.getPassword()==null)||bsCustomer.getPassword().equals(""))
       {
           this.addFieldError("bsCustomer.password", "<br/>密码不能为空！ ");
       }
       if((getPassword()==null)||getPassword().equals(""))
       {
           this.addFieldError("password", "<br/>验证密码不能为空！ ");
       }
       if(!bsCustomer.getPassword().equals(password))
       {
           this.addFieldError("password", "<br/>验证密码与设定不符合！ ");
       }
   }
   public String register() throws Exception
   {   
       msg=ERROR;
       bsCustomerService.setCustomerDao(new CustomerDao());       
       bsCustomerService.addCustomer(bsCustomer);
       msg=SUCCESS;
       ActionContext.getContext().getSession().put("bsCustomer",bsCustomer.getName()+"has registered");
       return msg;
   }
   public Customer getBsCustomer(){return bsCustomer;}
   public void setBsCustomer(Customer bsCustomer){this.bsCustomer=bsCustomer;}
  
}

